<template>
  <MainSlot v-loading="loading">
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>

    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :loading="loading"
        @selection-change="selectionChange">
        <template v-slot:filterValue="{ row }">
          <span class="pointer primary" v-if="row.filterType == 'DAY'">{{ `封塘后${row.filterValue}天做一次预防` }}</span>
          <span class="pointer primary" v-if="row.filterType == 'DATE'">
            <span v-if="row.filterCondition == 'WEEKLY'">{{
    `每周的${weekly[row.filterValue-1]}使用${row.materialName}做一次预防。` }}</span>
            <span v-if="row.filterCondition == 'MONTHLY'">{{
    `每月的${row.filterValue}日，使用${row.materialName}做一次预防。` }}</span>
            <span v-if="row.filterCondition == 'ANNUALLY'">{{
    `每年的${row.filterSupply}月${row.filterValue}日，使用${row.materialName}做一次预防。` }}</span>
          </span>
        </template>
      </TableData>
    </template>

    <template v-slot:pagination>
      <Pagination  @size-Change="handleSizeChange" :size="size"  @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>

    <template v-slot:dialogModule>
      <AddEdit :title="'预警'" @closeDialog="closeDialog" :type="taskTypeName" :facilityList="facilityList"
        :materialList="materialList" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import EnumSlot from '@/components/EnumSlot';
import AddEdit from './components/addEdit.vue';
import earlyWarningConfiguration from '@/api/earlyWarningConfiguration'
import facilityManagement from '@/api/digtalOperation/facilityManagement';
import materialApi from "@/api/basic-info-manage/agr-source-base-Manage"; // 物料

export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, EnumSlot, AddEdit },
  data() {
    return {
      taskTypeEnum: {
        DISINFECT: "消杀预警",
        FEED: "饲喂预警",
        OUTPUT: "产出预警",
        PREVENT: "预防预警",
        PROCESS: "过程预警",
      },
      weekly: ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
      taskTypeName: 'PREVENT',
      loading: false,
      dialogVisible: false,
      viewFlag: false,
      addEditFlag: 'add',
      formData: {},
      total: 100,
      page: 1,
      size:10,
      selectIndex: 0,
      btnList: ['add', 'delete'],
      dataList: [
        {
          item1: '设施1',
          item3: '10',
        }
      ],
      facilityList: [],
      materialList: [],
      tableThead: [
        { isShow: true, label: '养殖设施', prop: 'facilityName' },
        { isShow: true, label: '条件明细', slotName: 'filterValue' }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      }
    }
  },
  mounted() {
    this.queryPageByCondition();
    this.initData();
  },
  methods: {
    async initData() {
      //药品
      let { data: materialList } = await materialApi.getqueryList({});
      this.materialList = materialList;
      this.materialList = this.materialList.filter(data => {
        return data.materialTypeNo == '4165464165465146516' || data.materialTypeNo == '1077222993386450944'
      })
      //获取养殖设施；
      let { data: facilityList } = await facilityManagement.getFacilityListByConditionAll({
        breedType: sessionStorage.breedType
      });
      this.facilityList = facilityList || [];
    },
    delList(deleteList = [], key = 'earlyConfigId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      console.log(list)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await earlyWarningConfiguration.delList(list);
        if (res.code == 1) {
          this.$message.success(res.msg || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
          type: this.taskTypeName
        }
      }
      earlyWarningConfiguration.queryPageByCondition(params).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      this.page = 1;
      this.searchFormData ={...this.searchFormData,...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      } else if (item.flag == 'delete') {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || []
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>

<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}
</style>